package com.icat.house.web.intercept;

import com.alibaba.fastjson.JSONObject;
import com.icat.house.common.Constant;
import com.icat.house.model.UserInfo;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

public class LoginIntercept implements Interceptor {
	public void intercept(Invocation ai) {
		JSONObject result = new JSONObject();
		Controller controller = ai.getController();
		String token = controller.getPara("token");
		if (StrKit.notBlank(token) || !"null".equals(token)) {
			UserInfo info = UserInfo.validateToken(token);
			if (StrKit.notNull(info)) {
				controller.setAttr("user", info);
				ai.invoke();
				return;
			} else {
				result.put(Constant.ERROR, Constant.ERROR_MESSAGE_RE_ENTRY);
			}
		} else {
			result.put(Constant.ERROR, Constant.ERROR_MESSAGE_ENTRY);
		}
		controller.renderJson(result);
	}

}
